<?php
require_once('controller/auth.php');
//Start session
session_start();
//THis Comment is
//Include database connection details
require_once('controller/inc/config.inc');

//Array to store validation errors
$errmsg_arr = array();

//Validation error flag
$errflag = false;

//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
	die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
	die("Unable to select database");
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta content='text/html; charset=utf-8' http-equiv='Content-Type' />
<title>Project Tracking MIS</title>

<!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
<!--[if lt IE 9]>
<script src="https://html5shim.googlecode.com/svn/trunk/html5.js" type="text/javascript"></script>
<![endif]-->
<!-- Le styles -->
<link rel="stylesheet" href="assets/css/bootstrap.css" type="text/css" />
<link rel="stylesheet" href="assets/css/bootstrap-responsive.css"
	type="text/css" />
<link rel="stylesheet" href="assets/css/font-awesome.css"
	type="text/css" />
<script type="text/javascript" src="assets/js/jquery.min.js"></script>

<script type="text/javascript" src="assets/js/bootstrap.min.js"></script>
<script type="text/javascript" src="assets/js/bootstrap-popover.js"></script>
<script type="text/javascript" src="assets/js/bootstrap-tooltip.js"></script>
<script>
$(function ()
{ 
$("input").popover({trigger: 'focus', 'placement':'right'});

$("textarea").popover({trigger: 'focus'});


$('#tab').tooltip({'placement':'right', 'trigger' : 'hover'});

$('a').tooltip({'placement':'bottom', 'trigger' : 'hover'});	
});
</script>

<style>
body {
	padding-top: 60px;
	padding-bottom: 40px;
}

.sidebar-nav {
	padding: 9px 0;
}

.dropdown-menu .sub-menu {
	left: 100%;
	position: absolute;
	top: 0;
	visibility: hidden;
	margin-top: -1px;
}

.dropdown-menu li:hover .sub-menu {
	visibility: visible;
	display: block;
}

.navbar .sub-menu:before {
	border-bottom: 7px solid transparent;
	border-left: none;
	border-right: 7px solid rgba(0, 0, 0, 0.2);
	border-top: 7px solid transparent;
	left: -7px;
	top: 10px;
}

.navbar .sub-menu:after {
	border-top: 6px solid transparent;
	border-left: none;
	border-right: 6px solid #fff;
	border-bottom: 6px solid transparent;
	left: 10px;
	top: 11px;
	left: -6px;
}
</style>
<!-- Le fav and touch icons -->

</head>

<body>
<div class="container-fluid"><!--TOp Nav-->
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container-fluid"><a data-target=".nav-collapse"
	data-toggle="collapse" class="btn btn-navbar"> <span class="icon-bar"></span>
<span class="icon-bar"></span> <span class="icon-bar"></span> </a> <a
	href="#" class="brand">TMIS | Project Tracking</a>
<div class="nav-collapse">
<ul class="nav">
	<li class="active"><a href="dashboard.php">Home Page</a></li>
	<li><a href="create_project.php">Create Project</a></li>
	<li><a href="update_project.php">Update Project</a></li>
	<li><a href="reports.php">Reports</a></li>






</ul>
<form action="" class="navbar-search pull-left">
<input type="text" placeholder="Search" class="search-query span2">

</form>
<ul class="nav pull-right">


	<div class="pull-right">
	<ul class="nav pull-right">
		<li class="dropdown"><a href="#" class="dropdown-toggle"
			data-toggle="dropdown">Welcome, <?php echo $_SESSION['SESS_FIRST_NAME'] . " " . $_SESSION['SESS_LAST_NAME'];?>
		<b class="caret"></b></a>
		<ul class="dropdown-menu">
			<li><a href="/user/preferences"><i class="icon-cog"></i> Preferences</a></li>
			<li><a href="/help/support"><i class="icon-envelope"></i> Contact
			Support</a></li>
			<li class="divider"></li>
			<li><a href="controller/logout.php"><i class="icon-off"></i> Logout</a></li>
		</ul>
		</li>
	</ul>
	</div>


</ul>
</div>
<!-- /.nav-collapse --></div>
</div>
</div>








<script>
$(document).ready(function()
{
  //Handles menu drop down
  $('.dropdown-menu').find('form').click(function (e) {
        e.stopPropagation();
        });
  });
</script> <script type="text/javascript">
$(function() {
	//call the function onload
	getdata( 1 );
});

function getdata( pageno ){                     
	var targetURL = 'pagination/search_results.php?page=' + pageno;   

	$('#retrieved-data').html('<p><img src="pagination/ajax-loader.gif" /></p>');        
	$('#retrieved-data').load( targetURL ).hide().fadeIn('slow');
}      
</script> <!--/Top Nav-->
<div class="row-fluid">

<div class="span12"><!--Content-->


<h2 class='text-info'>Projects Status</h2>
<table class="table table-bordered">
<tr>
		
		<th>Project Name</th>
		
		<th>Prepared Date</th>
		<th>Expiration Day</th>
		<th>Project Status</th>
		<th>Overall Progress</th>
		<th colspan="2">Actions</th>
	</tr>
<?php
$select_proj=mysql_query("SELECT * FROM main WHERE status!=4");

//Ali sajjad Modifications Begins 

$index = 0;

//Ali sajjad Modifications Begins
/*Second Modification on 14th May
  Problem : Show status of preject in each level 
  Select Date colomn based on Over all Status of project and calculate the time window
*/		

while($row_proj=mysql_fetch_array($select_proj)){
$status=$row_proj['status'];
	$progress=0;
	switch($status){
	case 0:
	$status='Initiated';
	$progress="<div class='bar' style='width: 10%;' rel='tooltip' title='The overall progress of project is 10%!' data-placement='top'></div>";
	$start_DateColumOfSpecificStage = $row_proj['prepared_date'];
	//$end_DateColumOfSpecificStage = $row_proj['aprrove_ted_date'];
	$estimatedDays = $row_proj['est_day_ted'];
	$stat = $status;
	break;
	case 1:
	$status='Approved by MoE';
	$progress="<div class='bar' style='width: 30%;' rel='tooltip' title='The overall progress of project is 30%!' data-placement='top'></div>";
	$start_DateColumOfSpecificStage = $row_proj['submission_moe_date'];
	//$end_DateColumOfSpecificStage = $row_proj['approve_moe_date'];
	$estimatedDays = $row_proj['est_day_moe'];
	$stat = $status;
	break;
	case 2:
	$status='Budget Allocated';
	$progress="<div class='bar' style='width: 50%;' rel='tooltip' title='The overall progress of project is 50%!' data-placement='top'></div>";
	$start_DateColumOfSpecificStage = $row_proj['submission_moe_finance_date'];
	//$end_DateColumOfSpecificStage = $row_proj['eceived_moe_finace_date'];
	$estimatedDays = $row_proj['est_day_moe_fn'];
	$stat = $status;
	break;
	
	case 3:
	$status='Allotment Received';
	$progress="<div class='bar' style='width: 80%;' rel='tooltip' title='The overall progress of project is 80%!' data-placement='top'></div>";
	$start_DateColumOfSpecificStage = $row_proj['submission_mof_date'];
	//$end_DateColumOfSpecificStage = $row_proj['cheque_issue_date'];
	$estimatedDays = $row_proj['est_day_mof'];
	$stat = $status;
	break;
	
	case 4:
	$status='Done';
	$progress="<div class='bar bar-success' style='width: 100%;' rel='tooltip' title='Good Job, the overall progress of project is 100%!' data-placement='top'></div>";
	//$start_DateColumOfSpecificStage = $row_proj['prepared_date'];
	//$end_DateColumOfSpecificStage = $row_proj['aprrove_ted_date'];
	//$estimatedDays = $row_proj['est_day_ted'];
	$stat = $status;
	break;
	
	default:
	$status='Undefined!';
	$stat = $status;
	}
	?>
	
	<tr>
		
		<td><?php echo $row_proj['proj_name']; ?></td>
		<td><?php echo $row_proj['prepared_date']; ?></td>
		<?php
		
		
		if($stat != 4){

		
		
		//$pre_date=$row_proj['prepared_date'];
		$pre_date = $start_DateColumOfSpecificStage;
		
		list ($year,$month,$day)=explode('-', $pre_date);
		//$est_days=$row_proj['est_day_ted'];
		$est_days=$estimatedDays;
		
		
		@$exp_date=date('Y-m-d', mktime(0, 0, 0, $month, $day + $est_days, $year));
		//echo $exp_date;
		//echo $start_DateColumOfSpecificStage;
		?>
		<td><?php echo $exp_date; ?></td>
		
		<?php
		$current_date=date('Y-m-d');
		
		//Ali Sajjad Modification Begins
		$halfEstDays = ceil($est_days/2);
		list($cYear,$cMonth,$cDay)=explode('-',date('Y-m-d'));
		$xpDate1 = date('Y-m-d', mktime(0, 0, 0, $cMonth, $cDay - $halfEstDays, $cYear));
		@$xpDate2 = date('Y-m-d', mktime(0, 0, 0, $month, $day + $halfEstDays, $year));
		$date1 = strtotime($xpDate1);
		$date2 = strtotime($xpDate2);
		//$dateDiff = strtotime("5 days");
		$datetime1 = date_create("$cYear-$cMonth-$cDay");
		$datetime2 = date_create("$year-$month-$day");
		@$interval = date_diff($datetime2,$datetime1);
		//$cou=0;
		@$dateDiff = $interval->format("%R%a");
		//$cou++;
		//echo $xpDate1." - ".$xpDate2." Diff > ".$dateDiff."<br>";
		if($dateDiff <= $halfEstDays){
			if($dateDiff<=($halfEstDays/2)){

				$arrColor = "alert alert-success";
				$icon= "icon-ok";
			}
			else{
				$arrColor = "alert alert-warning";
				$icon= "icon-warning-sign";
				
			}
		}
		else{
		$arrColor = "alert alert-error";
		$icon="icon-remove-circle";
		}
		/*
			function secondsToTime1($inputSeconds) {
			
			    $secondsInAMinute = 60;
			    $secondsInAnHour  = 60 * $secondsInAMinute;
			    $secondsInADay    = 24 * $secondsInAnHour;
			
			    // extract days
			    $days = floor($inputSeconds / $secondsInADay);
			
			    // extract hours
			    $hourSeconds = $inputSeconds % $secondsInADay;
			    $hours = floor($hourSeconds / $secondsInAnHour);
			
			    // extract minutes
			    $minuteSeconds = $hourSeconds % $secondsInAnHour;
			    $minutes = floor($minuteSeconds / $secondsInAMinute);
			
			    // extract the remaining seconds
			    $remainingSeconds = $minuteSeconds % $secondsInAMinute;
			    $seconds = ceil($remainingSeconds);
			
			    // return the final array
			    $obj = array(
			        'd' => (int) $days,
			        'h' => (int) $hours,
			        'm' => (int) $minutes,
			        's' => (int) $seconds,
			    );
			    return $obj;
			}
		
		*/
		
		//$rtn = array();
		//$rtn = secondsToTime1($dateDiff);
		//$daysRem = $rtn['d'];
		
		
		//echo "<br>here<b>".$xpDate1." {$xpDate2} behind</b>";
		}//End of IF
		else{
			$arrColor = "alert alert-success";
				$icon= "icon-ok";
			
		}
		//Ali Sajjad Modification Ends
		
		
		?>
		<td class="<?php echo $arrColor; ?>"><?php echo $status; ?>&nbsp;&nbsp;<i class="<?php echo $icon;?>"></i></td>
		<td>
		<div class="progress progress-striped"><?php echo $progress; ?></div>
		</td>
		<td><a href="details.php?id=<?php echo $row_proj['serial']; ?>"><i class='icon-eye-open'></i>&nbsp;Details</a></td>
		<td><a href='update.php?id=<?php echo $row_proj['serial'];?>'><i class='icon-edit'></i>&nbsp;Update Project</a></td>
	</tr>
	</div>


	<?php

}

//Ali sajjad Modified Code
//echo "<br>";
//for($i=0;$i<count($arrColor);$i++)
//echo $arrColor[$i]."<br>";
//Ends
?>
</table>


<!--/Content--></div>


</div>
<hr />
<footer class="footer"> &copy;2013 TMIS, TED, MoE </footer></div>

</body>

</html>
